package transaction.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import transaction.domain.User;
import transaction.domain.UserRepository;

@Service("UserService")
public class UserServiceImpl implements UserService
{

	@Autowired
	UserRepository userRepository;

	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public User saveAndThrowException(User user)
	{
		userRepository.save(user);
		throw new RuntimeException("扔出一个异常");
	}

	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public User saveWithPropagationRequired(User user)
	{
		return userRepository.save(user);
	}

	@Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public User saveWithPropagationRequiresNew(User user)
	{
		return userRepository.save(user);
	}

	@Transactional(propagation = Propagation.MANDATORY, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public User saveWithPropagationMandatory(User user)
	{
		return userRepository.save(user);
	}

	@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public User saveWithPropagationSupports(User user)
	{
		return userRepository.save(user);
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public User saveWithPropagationNotSupported(User user)
	{
		return userRepository.save(user);
	}

	@Transactional(propagation = Propagation.NEVER, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
	public User saveWithPropagationNever(User user)
	{
		return userRepository.save(user);
	}

	@Override
	public void throwRunTimeException()
	{
		throw new RuntimeException("扔出去一个异常");
	}
}
